
#include "GN1621_Parts_Num.h"
#include "GN1621_Parts.h"
#include "GN1621.h"
#include "GN1621_Pin.h"
#include "../MCU_Mold/MCU_AbstractDigitron.h"
#include "UI.h"


const uint8_t TempAct_seg1[8][2] = 
{
    {COM_1,12 - 1}, //A
    {COM_1,11 - 1}, //B
    {COM_3,11 - 1}, //C
    {COM_4,12 - 1}, //D
    {COM_3,12 - 1}, //E
    {COM_2,12 - 1}, //F
    {COM_2,11 - 1}, //G
    {0,0}, //.
};
const uint8_t TempAct_seg2[8][2] = 
{
    {COM_1,10 - 1}, //A
    {COM_1,9 - 1}, //B
    {COM_3,9 - 1}, //C
    {COM_4,10 - 1}, //D
    {COM_3,10 - 1}, //E
    {COM_2,10 - 1}, //F
    {COM_2,9 - 1}, //G
    {0,0}, //.
};
const uint8_t TempAct_seg3[8][2] = 
{
    {COM_1,8 - 1}, //A
    {COM_1,7 - 1}, //B
    {COM_3,7 - 1}, //C
    {COM_4,8 - 1}, //D
    {COM_3,8 - 1}, //E
    {COM_2,8 - 1}, //F
    {COM_2,7 - 1}, //G
    {0,0}, //.
};

const uint8_t Temp_Unit_Icon[8][2] = 
{
    {COM_1,6 - 1}, //A
    {COM_1,5 - 1}, //B
    {COM_3,5 - 1}, //C
    {COM_4,6 - 1}, //D
    {COM_3,6 - 1}, //E
    {COM_2,6 - 1}, //F
    {COM_2,5 - 1}, //G
    {0,0}, //.
};

const uint8_t P1_seg1[8][2] = 
{ //百位
    {COM_1,30 - 1}, //A
    {COM_1,29 - 1}, //B
    {COM_3,29 - 1}, //C
    {COM_4,30 - 1}, //D
    {COM_3,30 - 1}, //E
    {COM_2,30 - 1}, //F
    {COM_2,29 - 1}, //G
    {0,0}, //.
};
const uint8_t P1_seg2[8][2] = 
{ //十位
    {COM_1,28 - 1}, //A
    {COM_1,27 - 1}, //B
    {COM_3,27 - 1}, //C
    {COM_4,28 - 1}, //D
    {COM_3,28 - 1}, //E
    {COM_2,28 - 1}, //F
    {COM_2,27 - 1}, //G
    {0,0}, //.
};
const uint8_t P1_seg3[8][2] = 
{ //个位
    {COM_1,26 - 1}, //A
    {COM_1,25 - 1}, //B
    {COM_3,25 - 1}, //C
    {COM_4,26 - 1}, //D
    {COM_3,26 - 1}, //E
    {COM_2,26 - 1}, //F
    {COM_2,25 - 1}, //G
    {0,0}, //.
};
const uint8_t P2_seg1[8][2] = 
{
    {COM_1,24 - 1}, //A
    {COM_1,23 - 1}, //B
    {COM_3,23 - 1}, //C
    {COM_4,24 - 1}, //D
    {COM_3,24 - 1}, //E
    {COM_2,24 - 1}, //F
    {COM_2,23 - 1}, //G
    {0,0}, //.
};
const uint8_t P2_seg2[8][2] = 
{
    {COM_1,22 - 1}, //A
    {COM_1,21 - 1}, //B
    {COM_3,21 - 1}, //C
    {COM_4,22 - 1}, //D
    {COM_3,22 - 1}, //E
    {COM_2,22 - 1}, //F
    {COM_2,21 - 1}, //G
    {0,0}, //.
};
const uint8_t P2_seg3[8][2] = 
{
    {COM_1,20 - 1}, //A
    {COM_1,19 - 1}, //B
    {COM_3,19 - 1}, //C
    {COM_4,20 - 1}, //D
    {COM_3,20 - 1}, //E
    {COM_2,20 - 1}, //F
    {COM_2,19 - 1}, //G
    {0,0}, //.
};
const uint8_t P3_seg1[8][2] = 
{
    {COM_1,18 - 1}, //A
    {COM_1,17 - 1}, //B
    {COM_3,17 - 1}, //C
    {COM_4,18 - 1}, //D
    {COM_3,18 - 1}, //E
    {COM_2,18 - 1}, //F
    {COM_2,17 - 1}, //G
    {0,0}, //.
};
const uint8_t P3_seg2[8][2] = 
{
    {COM_1,16 - 1}, //A
    {COM_1,15 - 1}, //B
    {COM_3,15 - 1}, //C
    {COM_4,16 - 1}, //D
    {COM_3,16 - 1}, //E
    {COM_2,16 - 1}, //F
    {COM_2,15 - 1}, //G
    {0,0}, //.
};
const uint8_t P3_seg3[8][2] = 
{
    {COM_1,14 - 1}, //A
    {COM_1,13 - 1}, //B
    {COM_3,13 - 1}, //C
    {COM_4,14 - 1}, //D
    {COM_3,14 - 1}, //E
    {COM_2,14 - 1}, //F
    {COM_2,13 - 1}, //G
    {0,0}, //.
};


void Lcd_Number_Unit(void)
{
    uint8_t x = 0;

    if(UI_System.Grill_Unit == Unit_C) x = Nixie_NumEncoder_C;
    else x = Nixie_NumEncoder_F;
    Lcd_Number(x, Temp_Unit_Icon);
}
void Lcd_Number(uint8_t num,uint8_t (*array)[2])
{
    uint8_t i;
    
    for (i = 0; i < 8; i++)
    {
        Cad_Dat.Cad_Com = array[i][0];
        Cad_Dat.Cad_Seg = array[i][1];
        Cad_Dat.Cad_Dis = (((Nixie_NumEncoder[num] >> i) + Nixie_Num_Inverse) & 0x01);
        GN_RAM_Refresh(Cad_Dat); 
    }
}
//Numbers
void Lcd_Numbers(uint32_t param,uint8_t (*array4)[2],uint8_t (*array3)[2],uint8_t (*array2)[2],uint8_t (*array1)[2])
{
    uint8_t temp;

    if((*array4) != NULL)
    {
        if(param == LcdSeg_Shine_OFF) temp = (LcdSeg_Shine_OFF & 0xf);
        else temp = (param / 1000) % 10;
        Lcd_Number(temp,array4);
    }
    if((*array3) != NULL)
    {
        if(param == LcdSeg_Shine_OFF) temp = (LcdSeg_Shine_OFF & 0xf);
        else temp = (param / 100) % 10;
        Lcd_Number(temp,array3);
    }
    if((*array2) != NULL)
    {
        if(param == LcdSeg_Shine_OFF) temp = (LcdSeg_Shine_OFF & 0xf);
        else temp = (param / 10) % 10;
        Lcd_Number(temp,array2);
    }
    if((*array1) != NULL)
    {
        if(param == LcdSeg_Shine_OFF) temp = (LcdSeg_Shine_OFF & 0xf);
        else temp = param % 10;
        Lcd_Number(temp,array1);
    }
}

void Lcd_NumShow(UI_Number_E t,int32_t param)
{
    uint16_t temp = 0;
    int32_t x = 0;
    int32_t x_1 = 0;
    int32_t x_2 = 0;
	
    x = param;
    if(param != LcdSeg_Shine_OFF)
    { 
        if(param < 0) param = -param;
        if(UI_System.Grill_Unit == Unit_C) param = F_Convert_C(param);
    }
    if(t == UI_RTD)
    {
        if(Temp_Param.oven_Err == Pt_Normal||Temp_Param.oven_Err == Pt_EOr||Temp_Param.oven_Err == Pt_EFn||Temp_Param.oven_Err == Pt_Elr)
        {
            x_1 = param / 100;
            x_2 = param / 10;
            if(x_2 == 0)
            {
                Lcd_Numbers(param,NULL,NULL,NULL,TempAct_seg3);
                Lcd_Number(Nixie_NumEncoder_NotShow,TempAct_seg1);
                Lcd_Number(Nixie_NumEncoder_NotShow,TempAct_seg2);
            } 
            else if(x_1 == 0)
            {
                Lcd_Numbers(param,NULL,NULL,TempAct_seg2,TempAct_seg3);
                Lcd_Number(Nixie_NumEncoder_NotShow,TempAct_seg1);
            }
            else Lcd_Numbers(param,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3);
            if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,TempAct_seg1);
        }
        else
        {
            if(Temp_Param.oven_Err <= Pt_HighTemp)
            {
                Lcd_Number(Nixie_NumEncoder_E_Char,TempAct_seg1);
                Lcd_Number(Nixie_NumEncoder_r,TempAct_seg2);
                if(Temp_Param.oven_Err == Pt_Open) temp = 0;
                else if(Temp_Param.oven_Err == Pt_Short) temp = 5;
                else if(Temp_Param.oven_Err == Pt_HighTemp) temp = Nixie_NumEncoder_H;
                else if(Temp_Param.oven_Err == Pt_LowTemp) temp = Nixie_NumEncoder_L;
                Lcd_Number(temp,TempAct_seg3);
            }
            /*else
            {
                if(Temp_Param.oven_Err == Pt_EOr)
                {
                    Lcd_Number(Nixie_NumEncoder_E_Char,TempAct_seg1);
                    Lcd_Number(Nixie_NumEncoder_r,TempAct_seg2);
                    Lcd_Number(Nixie_NumEncoder_A,TempAct_seg3);
                }
                if(Temp_Param.oven_Err == Pt_EFn)
                {
                    Lcd_Number(Nixie_NumEncoder_E_Char,TempAct_seg1);
                    Lcd_Number(Nixie_NumEncoder_r,TempAct_seg2);
                    Lcd_Number(Nixie_NumEncoder_F,TempAct_seg3);
                }
                if(Temp_Param.oven_Err == Pt_Elr)
                {
                    Lcd_Number(Nixie_NumEncoder_E_Char,TempAct_seg1);
                    Lcd_Number(Nixie_NumEncoder_r,TempAct_seg2);
                    Lcd_Number(Nixie_NumEncoder_i,TempAct_seg3);
                }
            }*/
        }
        Lcd_Set(Char_OFF);
        Lcd_Act(Char_ON);
    }
    else if(t == UI_RTDSettings)
    {
        x_1 = param / 100;
        x_2 = param / 10;
        if(x_2 == 0)
        {
            Lcd_Numbers(param,NULL,NULL,NULL,TempAct_seg3);
            Lcd_Number(Nixie_NumEncoder_NotShow,TempAct_seg1);
            Lcd_Number(Nixie_NumEncoder_NotShow,TempAct_seg2);
        } 
        else if(x_1 == 0)
        {
            Lcd_Numbers(param,NULL,NULL,TempAct_seg2,TempAct_seg3);
            Lcd_Number(Nixie_NumEncoder_NotShow,TempAct_seg1);
        }
        else Lcd_Numbers(param,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3);
        // Lcd_Numbers(param,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3);
        Lcd_Set(Char_ON);
        Lcd_Act(Char_OFF);
    }
    else if(t == UI_ProbeA)
    {
        if(RTD[ADC_Meat1].status == Pt_Normal)
        {
            x_1 = param / 100;
            x_2 = param / 10;
            if(x_2 == 0)
            {
                Lcd_Numbers(param,NULL,NULL,NULL,P1_seg3);
                Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg1);
                Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg2);
            }
            else if(x_1 == 0)
            {
                Lcd_Numbers(param,NULL,NULL,P1_seg2,P1_seg3);
                Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg1);
            }
            else Lcd_Numbers(param,NULL,P1_seg1,P1_seg2,P1_seg3);
            if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg1);
        }
        else
        {
            if(param == LcdSeg_Shine_OFF) Lcd_Numbers(param,NULL,P1_seg1,P1_seg2,P1_seg3);
            else
            {
                Lcd_Number(Nixie_NumEncoder_n,P1_seg1);
                Lcd_Number(Nixie_NumEncoder_o,P1_seg2);
                Lcd_Number(Nixie_NumEncoder_P,P1_seg3);
            }
        }
    }
    else if(t == UI_ProbeASettings)
    {
        if(RTD[ADC_Meat1].status == Pt_Normal)
        {
            // if(UI_System.State_Operation != UI_None) UI_System.Shine_ProbeA_Actual = Shine_ON;
            // if(UI_System.Shine_ProbeA_Actual == Shine_OFF)
            // {
            //     Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg1);
            //     Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg2);
            //     Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg3);
            // }
            // else
            // {
                x_1 = param / 100;
                x_2 = param / 10;
                if(x_2 == 0)
                {
                    Lcd_Numbers(param,NULL,NULL,NULL,P1_seg3);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg1);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg2);
                }
                else if(x_1 == 0)
                {
                    Lcd_Numbers(param,NULL,NULL,P1_seg2,P1_seg3);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg1);
                }
                else Lcd_Numbers(param,NULL,P1_seg1,P1_seg2,P1_seg3);
                if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg1);
            // }
        }
        else
        {
            Lcd_Number(Nixie_NumEncoder_n,P1_seg1);
            Lcd_Number(Nixie_NumEncoder_o,P1_seg2);
            Lcd_Number(Nixie_NumEncoder_P,P1_seg3);
        }
    }
    else if(t == UI_ProbeB)
    {
        if(UI_System.State_Operation != UI_None) 
        {
            Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg1);
            Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg2);
            Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg3);
        }
        else
        { 
            if(RTD[ADC_Meat2].status == Pt_Normal)
            {
                x_1 = param / 100;
                x_2 = param / 10;
                if(x_2 == 0)
                {
                    Lcd_Numbers(param,NULL,NULL,NULL,P2_seg3);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg1);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg2);
                } 
                else if(x_1 == 0)
                {
                    Lcd_Numbers(param,NULL,NULL,P2_seg2,P2_seg3);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg1);
                }
                else Lcd_Numbers(param,NULL,P2_seg1,P2_seg2,P2_seg3);
                // Lcd_Numbers(param,NULL,P2_seg1,P2_seg2,P2_seg3);
                if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg1);
            }
            else
            {
                Lcd_Number(Nixie_NumEncoder_n,P2_seg1);
                Lcd_Number(Nixie_NumEncoder_o,P2_seg2);
                Lcd_Number(Nixie_NumEncoder_P,P2_seg3);
            }
        }
    }
    else if(t == UI_ProbeC)
    {
        if(UI_System.State_Operation != UI_None) 
        {
            Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg1);
            Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg2);
            Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg3);
        } 
        else
        {
            if(RTD[ADC_Meat3].status == Pt_Normal)
            {
                x_1 = param / 100;
                x_2 = param / 10;
                if(x_2 == 0)
                {
                    Lcd_Numbers(param,NULL,NULL,NULL,P3_seg3);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg1);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg2);
                } 
                else if(x_1 == 0)
                {
                    Lcd_Numbers(param,NULL,NULL,P3_seg2,P3_seg3);
                    Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg1);
                }
                else Lcd_Numbers(param,NULL,P3_seg1,P3_seg2,P3_seg3);
                // Lcd_Numbers(param,NULL,P3_seg1,P3_seg2,P3_seg3);
                if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,P3_seg1);
            }
            else
            {
                Lcd_Number(Nixie_NumEncoder_n,P3_seg1);
                Lcd_Number(Nixie_NumEncoder_o,P3_seg2);
                Lcd_Number(Nixie_NumEncoder_P,P3_seg3);
            }
        }
    }
}
void Lcd_NumShow_OFF(void)
{
    Lcd_Number(Nixie_NumEncoder_o,TempAct_seg1);
    Lcd_Number(Nixie_NumEncoder_F,TempAct_seg2);
    Lcd_Number(Nixie_NumEncoder_F,TempAct_seg3);

    Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg1);
    Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg2);
    Lcd_Number(Nixie_NumEncoder_NotShow,P1_seg3);

    Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg1);
    Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg2);
    Lcd_Number(Nixie_NumEncoder_NotShow,P2_seg3);

    Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg1);
    Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg2);
    Lcd_Number(Nixie_NumEncoder_NotShow,P3_seg3);

    Lcd_Number(Nixie_NumEncoder_NotShow, Temp_Unit_Icon);
}

void Lcd_NumShow_Shine(uint32_t param)
{
    if(UI_System.Shine_Pause == 0)
    {
       if(UI_System.Shine_RTD_Setting == Shine_OFF) param = LcdSeg_Shine_OFF;
    }
    if(UI_System.Grill_state == State_Grill_SetTemp) Lcd_NumShow(UI_RTDSettings,param);
    else
    {
        if(UI_System.State_Operation == UI_RTDSettings) Lcd_NumShow(UI_RTDSettings,param);
        else if(UI_System.State_Operation == UI_ProbeASettings) Lcd_NumShow(UI_ProbeASettings,param);
    }
}

void Factory_Lcd_NumShow(UI_Number_E t,int32_t param)
{
    uint16_t temp = 0;
    int32_t x = 0;

    x = param;
    if(param < 0) param = -param;
    else if(param != LcdSeg_Shine_OFF) param = F_Convert_C(param);
    if(t == UI_RTD)
    {
        Lcd_Numbers(param,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3); 
        if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,TempAct_seg1);
    }
    else if(t == UI_ProbeA)
    {
        Lcd_Numbers(param,NULL,P1_seg1,P1_seg2,P1_seg3);
        if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,P1_seg1);
    }
    else if(t == UI_ProbeB)
    {
        Lcd_Numbers(param,NULL,P2_seg1,P2_seg2,P2_seg3);
        if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,P2_seg1);
    }
    else if(t == UI_ProbeC)
    {
        Lcd_Numbers(param,NULL,P3_seg1,P3_seg2,P3_seg3);
        if(x < 0) Lcd_Number(Nixie_NumEncoder_Crossbar,P3_seg1);
    }
}
